<?php
namespace app\command;
use app\admin\service\ImageService;
use app\admin\service\QrcodeService;
use app\admin\service\MailService;
use app\api\service\GongzonghaoService;
use think\cache\driver\Redis;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Option;
use think\console\input\Argument;
use think\Exception;
use app\admin\service\SmsService;
use think\Queue;
use fast\Random;
use app\common\library\Auth as UserAuth;
use app\admin\service\PhoneService;
class Mytest extends Command
{
    protected function configure()
    {
        $this->setName('Mytest')->setDescription('ceshi');
        $this ->addArgument("domain", Argument::OPTIONAL, "domain");   //参数可有可无   php think Test  openid123456

    }
    protected function execute(Input $input, Output $output)
    {

        $phoneServiceHandle = new PhoneService();
        $res = $phoneServiceHandle->previweInstallapp('VM010048254081',7);
        ppd($res);
       /* $sysConfig = getConfByType('basic');
        $app_ids = $sysConfig['app_ids'];
        if(empty($app_ids)){
            return true;
        }
        $app_idsArr = explode(',',$app_ids);
        $phoneServiceHandle = new PhoneService();
        foreach ($app_idsArr as $app_id){
            $res = $phoneServiceHandle->previweInstallapp('VM010048254081',$app_id);
            if($res == false){
                pp($phoneServiceHandle->errorMsg);
            }
            pp($res);
        }*/

        $phoneServiceHandle = new PhoneService();
        $rrr = $phoneServiceHandle->reset('VM010048254081');
        ppd($rrr,$phoneServiceHandle->errorMsg);

        $halfYearDates = $this->generateDateRange('two_years');
        ppd($halfYearDates);

//        $this->ceshiEmail();
//        $this->yasuoThumb();



        /*$auth = new UserAuth();
        $params = [];
        $admin_id = 13;
        $user_id = 9;
        $params['salt'] = Random::alnum();
        $params['password'] = '98723088';
        $params['password'] = $auth->getEncryptPassword($params['password'], $params['salt']);
        db('admin')->where('id',$admin_id)->update($params);
        db('user')->where('id',$user_id)->update($params);
        ppd($params['password'], $params['salt']);*/


      $recordData = db('user')
            ->where('id',">",0)->select();
        foreach ($recordData as $item){
            $uid = $item['id'];

            $qrcodeServiceHandle = new QrcodeService();
            $text = $qrcodeServiceHandle->getQrcodeText($uid);
            $qrcodePath = $qrcodeServiceHandle->makeQroce($text,$uid);
            $qrcodePath = str_replace(ROOT_PATH."public",'',$qrcodePath);
            db('user')->where('id',$uid)->update(['qrcode'=>$qrcodePath]);
        }
        ppd(22);
    }

    function generateDateRange($period) {
        $now = new \DateTime();
        $dates = [];
        $intervals = [
            'half_year' => 6, // 6 months
            'one_year' => 12, // 12 months
            'two_years' => 24, // 24 months
            'three_years' => 36, // 36 months
        ];


        // Clone the current date to avoid modifying the original $now object
        $endDate = clone $now;
        $startDate = clone $endDate;

        // Subtract the required number of months from the start date
        $startDate->modify('-' . $intervals[$period] . ' month');

        // Generate dates in the specified range
        for ($i = 0; $i <= $intervals[$period]; $i++) {
            // Clone the start date to avoid modifying it in the loop
            $date = clone $startDate;
            $date->modify('+' . $i . ' month');
            $dates[] = $date->format('Y-m');
        }

        return $dates;
    }
    function ceshiEmail(){


        //google专用密码
        //https://blog.csdn.net/guzarish/article/details/118626859
        //https://blog.csdn.net/moxun2011/article/details/82391568
        //465（需要SSL）、587（需要TLS）
//smtp-mail.outlook.com  587  STARTTLS
        //outlook.office365.com  993  tls
        $data = [];
        $data['email_host'] = "ssl://smtp.gmail.com:465";
//        $data['email_host'] = "smtp-mail.outlook.com";
//        $data['email_host'] = "smtp.qq.com";
//        $data['email_host'] = "smtpout.secureserver.net";

        $data['email_user'] = 'spcstudioltd@gmail.com';
        $data['email_pwd'] = 'spc2024032';

        //https://www.bilibili.com/read/cv29639684/
        //https://blog.51cto.com/fjcloud/9217410
//        $data['email_user'] = 'admin@auracrys.com';
//        $data['email_pwd'] = 'auracrys.com';


        $data['email_port'] = '25';//587  993
        $data['to_user'] = 'lamplook365@outlook.com';
        $data['to_user_name'] = 'kevin.kiu';
        $data['title'] = 'ceshiyieg 文章';
        $data['body'] = 'ceshiyieg 文章ceshiyieg 文章';
//        $email_data = json_decode($data,true);
        $MailService = new MailService();
        $res = $MailService->sendEmail2($data);
        ppd($res);
    }
    function getMonthsWorkDays($numMonthsBefore = 3, $numMonthsAfter = 3) {
        $result = [];
        $currentDate = new \DateTime(); // 获取当前日期
        $currentYear = $currentDate->format('Y');
        $currentMonth = $currentDate->format('n');

        // 计算开始和结束的月份
        $startMonth = max(1, $currentMonth - $numMonthsBefore);
        $endMonth = min(12, $currentMonth + $numMonthsAfter);

        // 遍历月份范围
        for ($month = $startMonth; $month <= $endMonth; $month++) {
            // 设置日期为指定年份和月份的第一天
            $firstDayOfMonth = new \DateTime("$currentYear-$month-01");
            $firstDayOfMonth->setTime(0, 0, 0); // 设置时间为当天开始

            // 计算到第一个星期一的偏移量
            $dayOfWeek = $firstDayOfMonth->format('N');
            $offset = ($dayOfWeek > 1) ? (8 - $dayOfWeek) : 0;

            // 设置开始日期为第一个星期一
            $startOfMonth = clone $firstDayOfMonth;
            $startOfMonth->modify("+$offset days");

            // 设置结束日期为最后一个工作日
            $endOfMonth = clone $startOfMonth;
            $endOfMonth->modify('last day of this month');


            // 将开始和结束日期添加到结果数组中
            $result[] = [
                'start' => $startOfMonth->format('Y-m-d'),
                'end' => $endOfMonth->format('Y-m-d')
            ];
        }
        if($currentMonth >= 10){
            $loopMonth = 3-(12-$currentMonth);
            $currentYear +=1;
            for ($month = 1; $month <= $loopMonth; $month++) {
                // 设置日期为指定年份和月份的第一天
                $firstDayOfMonth = new \DateTime("$currentYear-$month-01");
                $firstDayOfMonth->setTime(0, 0, 0); // 设置时间为当天开始

                // 计算到第一个星期一的偏移量
                $dayOfWeek = $firstDayOfMonth->format('N');
                $offset = ($dayOfWeek > 1) ? (8 - $dayOfWeek) : 0;

                // 设置开始日期为第一个星期一
                $startOfMonth = clone $firstDayOfMonth;
                $startOfMonth->modify("+$offset days");

                // 设置结束日期为最后一个工作日
                $endOfMonth = clone $startOfMonth;
                $endOfMonth->modify('last day of this month');


                // 将开始和结束日期添加到结果数组中
                $result[] = [
                    'start' => $startOfMonth->format('Y-m-d'),
                    'end' => $endOfMonth->format('Y-m-d')
                ];
            }
        }

        return $result;
    }

    public function yasuoThumb(){
//        $datas =db('subject')->where('id','>',0)->select();
        $datas =db('admin')->where('id','>',0)->select();
        foreach ($datas as $item){
            if(empty($item['avatar'])){
                continue;
            }
            if($item['avatar'] == '/assets/img/avatar.png'){
                continue;
            }
            $local_filePath = ROOT_PATH.'public'.$item['avatar'];
            $imageService = new ImageService();
            $imageService->compressImage($local_filePath);
        }

    }
    public function sms(){
        //https://e.3yit.com/http-document?spid=627476&pwd=d3c8a574207f66a32c9516eecf5d0641#aes
        $api = "https://api.3yit.com/api/send-sms-single";
        $data = [
            'sp_id'=>'627476',
            'mobile'=>'85296983508',
            'content'=>'【SPACESTUDIO】You are registering,the verification code is:2255,effective within 3 minutes',
//            'signature'=>'1',
            'password'=>md5('38c9f31d'),
        ];
        $head= [
            'nomerge',
            "Content-Type: application/x-www-form-urlencoded"
        ];

            //{"code":-1,"msg":"","data":"WL:NMLJ"}  错误
            //{"code":0,"msg":"success","msg_id":100046647}  成功
//        $res = $this->doCurl($data,$api);
        $res = requestHttpCurl($api,$data,$head);
        ppd($res);
    }

    function doCurl($data, $url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url );

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);

        curl_close($ch);

        return $output;
    }
}
